import numpy as np
import scipy.interpolate as inter
import random
import matplotlib.pyplot as plt

from matplotlib.font_manager import FontProperties
pic_font = FontProperties(fname=r"/System/Library/Fonts/PingFang.ttc", size=12) 

#plt.rcParams['font.sans-serif'] = ['PingFang']
#plt.rcParams['axes.unicode_minus'] = False

x = np.linspace(-5,5,500)
y = lambda x: x/(x**2+1)
x_1 = np.linspace(-5, 5, 10)
f1 = inter.interp1d(x_1, y(x_1), kind = 'nearest')
f2 = inter.interp1d(x_1, y(x_1), kind = 'linear')
t1 = plt.plot(x, f1(x), color = 'r' )
t2 = plt.plot(x, f2(x), color = 'b')
t3 = plt.plot(x, y(x), color = 'g')
t4 = plt.plot(x_1, y(x_1), '+', color = 'purple')

plt.legend((t1[0], t2[0], t3[0], t4[0]), ('最邻近一维插值', '分段线性一维插值', '原函数', '插值数据点'), prop={'fname':r"/System/Library/Fonts/PingFang.ttc",'size':9},loc='lower right')
plt.title('比较最临近一维插值和分段线性一维插值', FontProperties = pic_font)
plt.show()